package client_controllers;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;
import java.util.ArrayList;

import client_gui.ClassifyProductGUI;
import client_logic.CcrmClient;
import client_logic.dbHandler;
import entity.Boss;
import entity.Classification;

/**
 * @class ClassifyProductController ,in this class the marketing manager Pairing between product type and category.
 * 
 * @param ClassifyProductGUI links to GUI.
 * @param EmployeeLoginController links to employee controller. 
 * @param Boss is used as an instance, to send objects to DB and allow changes in the lists.
 * @param MarketingManagerController links to marketing manager controller.
 */
public class ClassifyProductController implements dbHandler {
	private Boss b;
	private ClassifyProductGUI cpg;
	private EmployeeLoginController elc;
	private MarketingManagerController mmc;
	
	public ClassifyProductController(ClassifyProductGUI cpg,EmployeeLoginController elc,MarketingManagerController mmc){
		this.b=new Boss(null,null,null);
		this.cpg=cpg;
		this.elc=elc;
		this.mmc=mmc;
		cpg.getBtnLogout().addActionListener(new Logout());
		cpg.addWindowListener(new Exit());
		cpg.getBtnBack().addActionListener(new Back());
		cpg.getBtnNext1().addActionListener(new Next1());
		cpg.getBtnNext2().addActionListener(new Next2());
		cpg.getBtnClassify().addActionListener(new Classify());
		getProducts();
		
	}
	public Boss getB() {
		return b;
	}
	public void setB(Boss b) {
		this.b = b;
	}
	public ClassifyProductGUI getCpg() {
		return cpg;
	}
	public void setCpg(ClassifyProductGUI cpg) {
		this.cpg = cpg;
	}
	public EmployeeLoginController getElc() {
		return elc;
	}
	public void setElc(EmployeeLoginController elc) {
		this.elc = elc;
	}
	public class Logout implements ActionListener {
        public void actionPerformed(ActionEvent ev){
        		setB(new Boss(getElc().getEmp(),"logout","ClassifyProductController"));
        		update();
        	}
	 }
	public class Back implements ActionListener {
        public void actionPerformed(ActionEvent ev){
        		cpg.dispose();
        		mmc.getMmg().setVisible(true);		
        	}
	 }
	
	/**
	 * @class next1 implements ActionListener use in getType() 
	 * 
	 */
	public class Next1 implements ActionListener {
        public void actionPerformed(ActionEvent ev){
        		getType();
        		cpg.getPanel_1().setVisible(true);	
        	}
	 }
	
	/**
	 * @class next2 implements ActionListener use in getCategory()
	 * 
	 */
	public class Next2 implements ActionListener {
        public void actionPerformed(ActionEvent ev){
        		getCategory();
        		cpg.getPanel_2().setVisible(true);	
        	}
	 }
	
	/**
	 * @class Classify implements ActionListener,save the classify in DB
	 * 
	 */
	public class Classify implements ActionListener {
        public void actionPerformed(ActionEvent ev){
        		Classification c=new Classification();
        		c.setProduct(cpg.getComboBoxProduct().getSelectedItem().toString());
        		c.setType(cpg.getComboBoxType().getSelectedItem().toString());
        		c.setCategory(cpg.getComboBoxCategory().getSelectedItem().toString());
        		b.setObj(c);
        		b.setCommand("classify");
        		b.setControl("ClassifyProductController");
        		update();
        	}
	 }
	@Override
	public void Add() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void Search() {
		try {
			CcrmClient.inet.setCurrCon(this);
			CcrmClient.inet.sendToServer(b);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	@Override
	public String Delete() {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	
	
	/**
	 * the function HandleMsg get massage from server
	 * @param msg it item from the DB Usually comes as Boss
	 */
	@Override
	public void HandleMsg(Object msg) {
		b=(Boss) msg;
		if(this.getB().getCommand().equals("logout")){
			this.getCpg().dispose();
			this.getElc().getElg().setVisible(true);
		}
		else if(this.getB().getCommand().equals("getproducts")){
			ArrayList <String> products ;
			products= (ArrayList<String>) this.getB().getObj();
			for(String i :products )
				cpg.getComboBoxProduct().addItem(i);
		}
		else if(this.getB().getCommand().equals("getcategorycombo")){
			ArrayList <String> categories ;
			categories= (ArrayList<String>) this.getB().getObj();
			for(String i :categories )
				cpg.getComboBoxCategory().addItem(i);
		}
		else if(this.getB().getCommand().equals("gettypeCombo")){
			ArrayList <String> types ;
			types= (ArrayList<String>) this.getB().getObj();
			for(String i :types )
				cpg.getComboBoxType().addItem(i);
		}
		else if(this.getB().getMsg().equals("Classification already exist")){
			cpg.getLblError().setText("Classification already exist");
			cpg.getLblError().setForeground(Color.red);
			cpg.getLblError().setVisible(true);
			
		}
		else if(this.getB().getMsg().equals("Classification successfull")){
			cpg.getLblError().setText("Classification Successfull");
			cpg.getLblError().setForeground(Color.blue);
			cpg.getLblError().setVisible(true);
		}
		
		
		
	}
	@Override
	public void update() {
		try {
			CcrmClient.inet.setCurrCon(this);
			CcrmClient.inet.sendToServer(b);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public class Exit implements WindowListener{

		@Override
		public void windowActivated(WindowEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void windowClosed(WindowEvent e) {
			
			
		}

		@Override
		public void windowClosing(WindowEvent e) {
			
			setB(new Boss(getElc().getEmp(),"logout","ClassifyProductController"));
    		update();
			
		}

		@Override
		public void windowDeactivated(WindowEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void windowDeiconified(WindowEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void windowIconified(WindowEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void windowOpened(WindowEvent e) {
			// TODO Auto-generated method stub
			
		}
		
	}
	
	/**
	 * This function getProducts() get products combo
	 */
	public void getProducts(){
		b.setCommand("getproducts");
		b.setControl("ClassifyProductController");
		Search();
	}
	
	/**
	 * This function getType() get types combo
	 */
	public void getType(){
		b.setCommand("gettypeCombo");
		b.setControl("ClassifyProductController");
		Search();
	}
	
	/**
	 * This function getCategory() get category combo
	 */
	public void getCategory(){
		b.setCommand("getcategorycombo");
		b.setControl("ClassifyProductController");
		Search();
	}
}
